import time
import pytest
import allure
import TestCase

from Base.InitDriver import init_driver
from Page.CommonPage import CommonPage
from Utils.XiaoMaUtils import XiaoMaUtils


from Utils.PerfDogUtils import PerfDogUtils


@allure.feature('TestCase_27: 导航1H消耗的流量在合理范围之内	')
@allure.issue(
    'https://www.tapd.cn/56767367/sparrow/tcase/view/1156767367001062271?url_cache_key=ab49d6eabe9823050826460ca132aea4')
class TestCaseSpecial27:
    def setup_class(self):
        self.driver = init_driver()
        # 实例化页面
        # 桌面
        self.launcher_index_page = CommonPage(self.driver).get_launcher_index_page()
        self.launcher_app_list_page = CommonPage(self.driver).get_launcher_app_list_page()
        # 地图
        self.map_index_page = CommonPage(self.driver).get_map_index_page()
        self.map_navigating_page = CommonPage(self.driver).get_map_navigating_page()
        self.map_route_select_page = CommonPage(self.driver).get_map_route_select_page()
        self.map_search_page = CommonPage(self.driver).get_map_search_page()
        self.map_search_result_page = CommonPage(self.driver).get_map_search_result_page()
        self.map_simulation_page = CommonPage(self.driver).get_map_simulation_page()

        # 实例化PerfDogUtils的工具类
        # 开始监控
        self.perfDogUtils = PerfDogUtils()
        # # 输入需监控的应用名, 如"xting", "music"
        self.perfDogUtils.start_test("navi")

    def teardown_class(self):
        self.perfDogUtils.stop_test()
        XiaoMaUtils.close_map('navi')
        XiaoMaUtils.close_app('launcher')
        self.driver.quit()

    @allure.story('step1:点击应用页,点击地图图标')
    @pytest.mark.run(order=271)
    def test_special_27_1_click_list(self):
        try:
            self.launcher_index_page.click_app_list()
            time.sleep(5)
            self.launcher_app_list_page.click_appname(TestCase.MAP)
            time.sleep(5)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story('step2:进入地图,查看页面加载完成')
    @pytest.mark.run(order=272)
    def test_special_27_2_click_list(self):
        try:
            text1 = self.map_index_page.is_display_map_index()
            if text1 == True:
                print("进入应用成功")
            else:
                print("点击应用出现问题,无法进入")

        except Exception as e:
            raise e

        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story('step3:点击搜索,输入任意地点后进行搜索,然后选择第一个地点,选择导航,然后选择模拟导航')
    @pytest.mark.run(order=273)
    def test_special_27_3_click_list(self):
        try:
            self.map_index_page.click_search()
            text1 = self.map_search_page.is_display_map_search()
            print(text1)
            if text1 == True:
                self.map_search_page.click_bar()
                self.map_search_result_page.input_bar(index='深圳')
                time.sleep(3)
                text2 = self.map_search_result_page.is_display_map_result()
                if text2 == True:
                    self.map_search_result_page.click_navigation_now()
                    text3 = self.map_route_select_page.is_display_map_route()
                    print(text3)
                    if text3 == True:
                        self.map_route_select_page.click_more()
                        text4 = self.map_simulation_page.is_display_map_simulation()
                        if text4 == True:
                            self.map_simulation_page.click_navigation()
                            text5 = self.map_navigating_page.is_display_map_navigating()
                            if text5 == True:
                                print("模拟导航成功")
                            else:
                                print("模拟导航失败")
                        else:
                            print("暂无模拟导航按钮")
                    else:
                        print("立即导航按钮无效")
                else:
                    print("无法进入搜索结果页")
            else:
                print("点击搜索按钮失效")

        except Exception as e:
            raise e

        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

    @allure.story('step4:等待一个小时,查看流量消耗')
    @pytest.mark.run(order=274)
    def test_special_27_4_wait(self):
        try:
            print("当前正在测试流量,请不要结束进程和更改页面")
            time.sleep(30)
            # time.sleep(3600)
        except Exception as e:
            raise e
        finally:
            allure.attach(self.driver.get_screenshot_as_png(), XiaoMaUtils.get_current_time(),
                          allure.attachment_type.PNG)

if __name__ == 'main__':
    # pass
    pytest.main("-s testcase_special_map_27.py")
